package com.hspedu.homework;

/**
 * @author 谷其昊
 * @version 1.0
 * @date 2022/2/16
 * @time 14:56
 */
public class Homework02 {
    public static void main(String[] args) {
        Count count = new Count();
        Thread thread = new Thread(count);
        Thread thread1 = new Thread(count);
        thread.setName("t0");
        thread1.setName("t1");

        thread1.start();
        thread.start();
    }
}

class Count implements Runnable{
    private int totalMoney = 10000;
    private int num = 0;
    @Override
    public void run() {
        withdraw();
    }

    public synchronized void withdraw(){
        while (true){
            if (totalMoney <= 0){
                break;
            }
            totalMoney -= 1000;
            System.out.println(Thread.currentThread().getName()+"第"+(++num)+"次取钱，账户还剩下"+(totalMoney)+"元");
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
